package com.cma.cmabackend.service;

import com.cma.cmabackend.dao.ParameterDAO;
import com.cma.cmabackend.entity.LabEmp;
import com.cma.cmabackend.entity.Parameter;
import com.cma.cmabackend.entity.Project;
import com.cma.cmabackend.entity.Standard;
import com.cma.cmabackend.entity.re.ReDetailParameter;
import com.cma.cmabackend.entity.re.ReParameter;
import com.cma.cmabackend.entity.re.ReUser;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ParameterService {
    @Resource
    StandardService standardService;
    @Resource
    LabEmpService labEmpService;
    @Resource
    ParameterDAO parameterDAO;
    @Resource
    TypeService typeService;
    @Resource
    CategoryService categoryService;

    @Resource
    ProjectService projectService;

    @Transactional
    public Parameter add(String name, Integer standardId, List<Integer> emps) {
        Parameter parameter = new Parameter(name);
        parameter.setStandard(standardService.findById(standardId));
        parameter.setEmps(new ArrayList<>());
        for (Integer i : emps) {
            parameter.getEmps().add(labEmpService.findById(i));
        }
        return parameterDAO.save(parameter);
    }

    public List<Parameter> findAll() {
        return parameterDAO.findAll();
    }

    public List<Parameter> findBystandardId(Integer x) {
        return parameterDAO.findByStandard_Id(x);
    }

    public List<ReDetailParameter> findByEmpIdAndStatus(Integer x, Parameter.Status status) {
        List<Parameter> parameterList = parameterDAO.findByEmpsAndStatusIs(labEmpService.findById(x), status);
        List<ReDetailParameter> reParameterList = new ArrayList<>();
        for (Parameter p : parameterList) {
            ReDetailParameter reDetailParameter = new ReDetailParameter(p.getId(), categoryService.getCategoryNameById(p.getStandard().getCategoryId()), typeService.getTypeNameById(p.getStandard().getTypeId()), p.getName(), p.getLabAdmin().getUsername(), p.getStandard().getName(), p.getStandard().getId());
            reParameterList.add(reDetailParameter);
        }
        return reParameterList;
    }

    public ReParameter findById(Integer id) {
        Parameter parameter = parameterDAO.findById(id).orElse(null);
        if (parameter == null) {
            return null;
        }
        ReParameter reParameter = new ReParameter(parameter.getId(), parameter.getName(), new ArrayList<>(), parameter.getHumanList(), parameter.getEquipmentList(), parameter.getSample(), parameter.getSop(), parameter.getFacilityList(), parameter.getCompareValidList(), parameter.getSimulation(), parameter.getStatus());
        for (LabEmp u : parameter.getEmps()) {
            reParameter.getEmps().add(new ReUser(u.getId(), u.getType(), u.getUsername()));
        }
        return reParameter;
    }

    public Parameter save(Parameter parameter) {
        return parameterDAO.save(parameter);
    }

    public void changeStatus(Integer id) {
        Parameter parameter = parameterDAO.findById(id).orElse(null);
        if (parameter != null) {
            parameter.setStatus(Parameter.Status.completed);
            parameterDAO.save(parameter);
        } else {
            return;
        }
        Standard standard = standardService.findByParameter(parameter);
        Project project = projectService.findByStandard(standard);
        for (Standard s : project.getStandards()) {
            for (Parameter p : s.getParameterList()) {
                if (p.getStatus() == Parameter.Status.Uncompleted) {
                    return;
                }
            }
        }
        project.setStatus(Project.Status.Completed);
        projectService.save(project);
    }

    public Parameter findRealById(Integer x) {
        return parameterDAO.findById(x).orElse(null);
    }

    public void sendBack(Integer id, Integer project) {
        Project project1 = projectService.findRealById(project);
        project1.setStatus(Project.Status.Progressing);
        projectService.save(project1);
        Parameter parameter = parameterDAO.findById(id).orElse(null);
        if (parameter == null) {
            return;
        }
        parameter.setStatus(Parameter.Status.Uncompleted);
        parameterDAO.save(parameter);
    }
}
